from copy import deepcopy


class Solution:
    def uniquePathsWithObstacles(self, obstacleGrid) -> int:
        res_list, flag_i, flag_j = deepcopy(obstacleGrid), True, True
        for i in range(0, len(res_list)):
            if flag_i and obstacleGrid[i][0] != 1:
                res_list[i][0] = 1
            else:
                res_list[i][0] = 0
                flag_i = False
        for j in range(0, len(res_list[0])):
            if flag_j and obstacleGrid[0][j] != 1:
                res_list[0][j] = 1
            else:
                res_list[0][j] = 0
                flag_j = False
        for i in range(1, len(res_list)):
            for j in range(1, len(res_list[0])):
                if obstacleGrid[i][j] == 1 or (obstacleGrid[i-1][j] == 1 and obstacleGrid[i][j-1] == 1):
                    res_list[i][j] = 0
                elif obstacleGrid[i-1][j] == 1:
                    res_list[i][j] = res_list[i][j-1]
                elif obstacleGrid[i][j-1] == 1:
                    res_list[i][j] = res_list[i-1][j]
                else:
                    res_list[i][j] = res_list[i][j - 1] + res_list[i-1][j]
        # print(res_list)
        return res_list[-1][-1]


a = Solution()
print(a.uniquePathsWithObstacles([[0, 0, 0],
                                  [0, 1, 0],
                                  [0, 0, 0]]))